<?php
/***
 *人事部

 */
namespace App\Http\Controllers\admin;

use App\model\Attendance;
use App\model\Leave;
use App\model\Social;
use App\model\User;
use App\model\Position;
use App\model\Wages;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Http\Requests;

class HumanController extends Controller
{
    /***
     *个人信息
     */
    public function information(Request $request){
/*        $aa=date('Y-m-01 00:00:00 ',time());
        $now=strtotime($aa);
        $shang=date('m')-1;
        $bb=date('Y-'.$shang.'-01 00:00:00 ',time());
        $last=strtotime($bb);
        $fine=DB::table('fine')->where('fin_time','>=',$last)->where('fin_time','<',$now)->get();*/
        $model= new User();
        $user_id=$request->session()->get('use_id');
        if($request->all("_token")){
            $arr=$request->all();
            $edit=$model->edituser($arr,$user_id);
            if($edit){
                return redirect('human/information');
            }
        }else{
            $user=$model->getData($user_id);
            return view('human.information',compact('user'));
        }
    }
    /***
     *新增员工
     */
    public function add(){
        return view('human.add');
    }
    /***
     *员工维护
     * 作者：*****
     */
    public function maintain(Request $request){
            $model=new User();
        $page=$request->get('page');
        if($request->ajax()&&$request->get('type')==1){
            $id=$request->get('id');
            $quit=$model->quit($id);
            if($quit){
                return 1;
            }
        }elseif($request->ajax()&&$request->get('type')==2){
            $search=$request->all();
            $arr=$model->search($search);
            return  view('human.search',compact('arr'));
        }elseif($request->all()&&!isset($page)){
            $excel=$request->all();
            $arr=$model->search($excel);
            header('Content-type:application/vnd.ms-excel');
            header('Content-Disposition:attachment;filename=员工维护.xls');
            $str="序号\t姓名\t编号\t身份证号\t出生年月\t岗位\t合同结束日期\t联系电话\n";
            foreach($arr as $a){
                $str.="$a->use_id\t$a->use_name\t$a->use_num\t$a->use_card\t$a->use_birthday\t$a->use_position\t$a->use_contractend\t$a->use_phone\n";
            }
            return   iconv('utf-8','gbk',$str);
        }
        else{
            $position=new Position();
            $post=$position->lists();
            $user=$model->lists();
            return view('human.maintain',compact('user','post'));
        }
    }
    /***
     *员工维护修改页面
     * 作者：*****
     */
    public function maintainedit(Request $request){
        $id=$request->get('id');
        $model = new User();
        if(!empty($id)&&isset($id)){
            $arr=$model->maintainedit($id);
            return view('human.maintainedit',compact('arr'));
        }
        if($request->all()){
            $arr=$request->all();
            $ok=$model->maintaineditok($arr);
            if($ok){
                return redirect('human/maintain');
            }else{
                return view('errors.404');
            }
        }
    }
    /***
     *员工考勤
     */
    public function attendance(){
        $model= new User();
        $arr=$model->attlist();
        return view('human.attendance',compact('arr'));
    }
    /***
     *员工请假
     */
    public function leave(Request $request){
        $lea=new Leave();
        if($request->ajax()&&$request->get('type')==1){
            $id=$request->get('id');
            $le=$lea->leave($id);
            if($le){
                return 1;
            }
        }
        $model= new User();
        $leave=$model->leave();
        return view('human.leave',compact('leave'));
    }
    /***
     *教练学时
     */
    public function hours(){
        $model=new User();
        $info=$model->attlist();
        return view('human.hours',compact('info'));
    }
    /***
     *社保详情
     */
    public function social(){
        $model=new Social();
        $social=$model->lists();
        return view('human.social',compact('social'));
    }
    /***
     *工资设置
     */
    public function salarysetup(){
        $model=new Wages();
        $wages=$model->lists();
        return view('human.salarysetup',compact('wages'));
    }
}
